package yns.springboot.security.sample.service.impl;

import java.util.ArrayList;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import yns.springboot.security.sample.base.service.impl.BaseServiceImpl;
import yns.springboot.security.sample.dao.RoleDao;
import yns.springboot.security.sample.dto.RoleDto;
import yns.springboot.security.sample.entity.RoleEntity;
import yns.springboot.security.sample.page.PaginationInfo;
import yns.springboot.security.sample.service.RoleService;

@Service
public class RoleServiceImpl extends BaseServiceImpl implements RoleService {

	@Autowired
	private RoleDao roleDao;

	@Override
	public List<RoleDto> getRole() {
		List<RoleEntity> listRoleEntity = roleDao.getRole();
		List<RoleDto> listRoleDto = new ArrayList<RoleDto>();

		listEntityToDto(listRoleEntity, listRoleDto);

		return listRoleDto;
	}

	@Override
	public int addRole(RoleDto roleDto) {
		RoleEntity roleEntity = new RoleEntity();
		transformDtoToEntity.parseObject(roleDto, roleEntity);

		return roleDao.addRole(roleEntity);
	}

	@Override
	public RoleDto getRoleByID(int id) {
		RoleEntity roleEntity = roleDao.getRoleByID(id);
		RoleDto roleDto = new RoleDto();
		transformEntityToDto.parseObject(roleEntity, roleDto);

		return roleDto;
	}

	@Override
	public int getTotalCount() {
		return roleDao.getTotalCount();
	}

	@Override
	public List<RoleDto> getPage(int currentPage, int pageSize) {

		PaginationInfo<RoleEntity> paginationInfo = new PaginationInfo<RoleEntity>();
		paginationInfo.setCurrentPage(currentPage);
		paginationInfo.setPageSize(pageSize);

		PaginationInfo<RoleEntity> pagingResults = pagination.paging(roleDao, paginationInfo);
		List<RoleEntity> listRoleEntity = pagingResults.getDataList();

		List<RoleDto> listRoleDto = new ArrayList<RoleDto>();

		listEntityToDto(listRoleEntity, listRoleDto);

		return listRoleDto;
	}

	@Override
	public void listEntityToDto(List<RoleEntity> listEntity, List<RoleDto> listDto) {

		RoleDto dto;
		for (RoleEntity entity : listEntity) {
			dto = new RoleDto();
			transformEntityToDto.parseObject(entity, dto);
			listDto.add(dto);
		}

	}

	@Override
	public void listDtoToEntity(List<RoleDto> listDto, List<RoleEntity> listEntity) {

		RoleEntity entity;
		for (RoleDto dto : listDto) {
			entity = new RoleEntity();
			transformDtoToEntity.parseObject(dto, entity);
			listEntity.add(entity);
		}

	}

	@Override
	public List<RoleDto> search(int currentPage, int pageSize, RoleDto dto) {
		// TODO Auto-generated method stub
		return null;
	}

}
